package ytdp.careagle.com.ytdp.activity;

import android.inputmethodservice.Keyboard;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.analytics.dplus.UMADplus;

import butterknife.Bind;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.mvp.presenter.LoginPresenter;
import ytdp.careagle.com.ytdp.mvp.view.ILoginView;
import ytdp.careagle.com.ytdp.myview.MyEditText;
import ytdp.careagle.com.ytdp.myview.MyKeyboradView;

public class LoginActivity extends BaseActivity implements ILoginView {

    @Bind(R.id.iv_logo)
    ImageView ivLogo;
    @Bind(R.id.tv_welcome_use)
    TextView tvWelcomeUse;
    @Bind(R.id.et_username)
    MyEditText etUsername;
    @Bind(R.id.iv_username_clear)
    ImageView ivUsernameClear;
    @Bind(R.id.et_password)
    MyEditText etPassword;
    @Bind(R.id.iv_password_clear)
    ImageView ivPasswordClear;
    @Bind(R.id.tv_login)
    TextView tvLogin;
    @Bind(R.id.tv_version)
    TextView tvVersion;
    @Bind(R.id.myKeyboradView)
    MyKeyboradView myKeyboradView;
    private LoginPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        UMADplus.track(this, "登录页面", null);
        tvVersion.setText("Version " + G.APPVERSION);
        //顺序要先获取焦点的放最后
        myKeyboradView.setEdittext(etPassword, MyKeyboradView.KEYBOARD_STATE_ENGLISH);
        myKeyboradView.setEdittext(etUsername, MyKeyboradView.KEYBOARD_STATE_NUMBER);
        presenter = new LoginPresenter(this);
        //一开始不让软禁盘弹出
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = etPassword.getText().toString();
                if (!TextUtils.isEmpty(s)) {

                    ivPasswordClear.setVisibility(View.VISIBLE);
                } else {
                    ivPasswordClear.setVisibility(View.GONE);
                }
            }
        });
        etUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = etUsername.getText().toString();
                if (!TextUtils.isEmpty(s)) {
                    ivUsernameClear.setVisibility(View.VISIBLE);
                } else {
                    ivUsernameClear.setVisibility(View.GONE);
                }
            }
        });
        etPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    ivUsernameClear.setVisibility(View.GONE);
                    if (!TextUtils.isEmpty(etPassword.getText())) {
                        ivPasswordClear.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        etUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    ivPasswordClear.setVisibility(View.GONE);
                    if (!TextUtils.isEmpty(etUsername.getText())) {
                        ivUsernameClear.setVisibility(View.VISIBLE);
                    }
                }
            }
        });


    }

    @OnClick({R.id.tv_login, R.id.iv_password_clear, R.id.iv_username_clear})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                presenter.goLogin(getWContext().get());
                break;
            case R.id.iv_password_clear:
                etPassword.setText("");
                break;
            case R.id.iv_username_clear:
                etUsername.setText("");
                break;
        }
    }

    @Override
    public String getUsername() {
        return etUsername.getText().toString();
    }

    @Override
    public String getPassword() {
        return etPassword.getText().toString();
    }

    @Override
    public void showLoginSuccess() {
        getOperation().forwardClearTop(CarEvaluateQueryActivity.class);
        finish();
    }

    @Override
    public void setLogin(boolean b) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && myKeyboradView.getTranslationY() == 0) {
            myKeyboradView.closeKeyboard();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
